package com.xiao.dynamic_rule.datagen;


import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * hbase创建表命令： create 'user_profile','f'(表名，列族)
 */
public class UserProfileDataGen {
    public static void main(String[] args) throws IOException {

        //建立hbase的连接
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","hadoop101:2181,hadoop102:2181,hadoop103:2181");
        Connection conn = ConnectionFactory.createConnection(conf);
        Table tableName = conn.getTable(TableName.valueOf("user_profile"));


        //生成一百万条标签画像数据,每个用户1000个标签
        for (int i=1;i<=10000;i++){

            ArrayList<Put> list = new ArrayList<>();
            //优化：一批一批的写入，速度快些
            for (int j=0;j<20;j++){
                //生成标签画像数据 rowkey 标签值1，标签值2，。。。。。。。。。。。标签值1000
                String deviceId = StringUtils.leftPad(i + "", 6, "0");
                Put puts = new Put(Bytes.toBytes(deviceId));
                for (int k=0;k<=1000;k++){
                    String key = "profileKey" + k;
                    String value = "profileValue" + RandomUtils.nextInt(1,1000);
                    //使用hbase的Put一行一行写入hbase
                    puts.addColumn(Bytes.toBytes("f"),Bytes.toBytes(key),Bytes.toBytes(value));
                }
                list.add(puts);
            }
            //提交一批
            tableName.put(list);
            list.clear();

        }
        //关闭连接
        conn.close();
    }
}
